package com.example.whm;

import java.util.ArrayList;
import java.util.List;

import org.holoeverywhere.app.Application;
import org.holoeverywhere.preference.SharedPreferences;

import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import cn.db.DaoMaster;
import cn.db.DaoMaster.DevOpenHelper;
import cn.db.DaoSession;
import cn.db.MsgDao;

import com.alibaba.fastjson.JSON;
import com.androidquery.AQuery;
import com.androidquery.callback.AjaxStatus;
import com.androidquery.callback.BitmapAjaxCallback;
import com.androidquery.util.AQUtility;
import com.example.whm.lib.Service_;
import com.example.whm.lib.client;



public class Whm_app extends Application {

	static String api_url="192.168.16.11";
	//static String api_url="192.168.2.104";

	//static String api_url="192.168.16.69";
	static String api_url_pref="http://192.168.16.11:9000";
	//static String api_url_pref="http://192.168.2.104:9000";
	//static String api_url="192.168.2.104";
   
	static List<Integer> newl=new ArrayList<Integer>();
	SharedPreferences sp;
	static DaoSession daoSession;
	@Override
	public SharedPreferences getDefaultSharedPreferences() {
		// TODO Auto-generated method stub
		return Whm_app.this.getSharedPreferences("whm",Context.MODE_PRIVATE);
	}
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
	 
		AQuery a=new AQuery(this);
		String url = "http://"+Whm_app.api_url+":9000/api/init";
		a.ajax(url, String.class,this,"parsedata");
		
		sp=getSharedPreferences("whm",Context.MODE_PRIVATE);
		client.pre_cache_put(sp, "host", api_url); 
	    startService(new Intent(Whm_app.this, Service_.class));
		init_db();
	}
	
	public void init_db(){
		DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "whm.db", null);
        SQLiteDatabase db = helper.getWritableDatabase();
        DaoMaster daoMaster = new DaoMaster(db);
         daoSession = daoMaster.newSession();
       
		
	}
	
	
	public void parsedata(String url, String s, AjaxStatus status){
    	final String s2=s;
    	if (status.getCode()==200) {
    		
    		 new Thread( new Runnable() {
    	         @Override
				public void run() {
    	        	
    	        	 String re=JSON.parseObject(s2).getString("result");
    	     		String ids=JSON.parseObject(re).getString("ids");
    	    	   client.pre_cache_put(Whm_app.this.getSharedPreferences("whm",Context.MODE_PRIVATE), "ids", ids); 
    	         }}).start();
    		
    		
    		
		}else{
			
		}
    	
    	
    }
	
	
	
	
	
	@Override
	public void onLowMemory() {
		// TODO Auto-generated method stub
		super.onLowMemory();
		BitmapAjaxCallback.clearCache();
		AQUtility.cleanCacheAsync(this);
	}
	
	public Whm_app() {
		super();
		// TODO Auto-generated constructor stub
	} 
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
